<?php

/*
	2015.5.27  yangliang 新增，去除ucenter，将ucenter注册验证代码移至此处
	[SupeSite] (C) 2007-2009 Comsenz Inc.
	$Id: admin.func.php 13305 2009-08-31 05:33:01Z zhaofei $
*/

if(!defined('IN_SUPESITE')) {
	exit('Access Denied');
}
@include_once(S_ROOT.'./class/Easemob.class.php');
function check_username($username) {
	$guestexp = '\xA1\xA1|\xAC\xA3|^Guest|^\xD3\xCE\xBF\xCD|\xB9\x43\xAB\xC8';
	$len = strlen($username);
	if($len > 15 || $len < 3 || preg_match("/\s+|^c:\\con\\con|[%,\*\"\s\<\>\&]|$guestexp/is", $username)) {
		return FALSE;
	} else {
		return TRUE;
	}
}

function check_phonenumexists($phonenum){
    $_POST['phone'] = $phonenum;
    $modelClass = 'app\com\simtoon\models\User';
    $data = YiiComm::executeByMethod($modelClass,'CheckUser',$_POST);
    $res = $data->getModels();
    if(count($res) > 0){
        $data = 1;
    }else{
        $data = 0;
    }
//    global $_SGLOBAL;
//    $query = $_SGLOBAL['db']->query('SELECT uid FROM '.tname('user').' WHERE phonenum=\''.$phonenum.'\'');
//    $data = @mysql_result($query, 0);
    return $data;
}



//为第三方公司提供找回密码--检测是否为第三方公司员工手机号
function check_otherphonenumexists($phonenum){
    $_POST['phone'] = $phonenum;
    $modelClass = 'app\com\simtoon\models\User';
    $data = YiiComm::executeByMethod($modelClass,'CheckAgent',$_POST);
    $res = $data->getModels();
    if(count($res) > 0){
        $data = 1;
    }else{
        $data = 0;
    }
//    global $_SGLOBAL;
//    $query = $_SGLOBAL['db']->query('SELECT uid FROM '.tname('user').' WHERE phonenum=\''.$phonenum.'\'');
//    $data = @mysql_result($query, 0);
    return $data;
}



function check_phonenum($phonenum){
    if(preg_match("/1[34578]{1}\d{9}$/",$phonenum)){
       return true;
    }else{
       return false;
    }
}


function get_user_by_username($phonenum) {

    $_POST['phone'] = $phonenum;
    $modelClass = 'app\com\simtoon\models\User';
    $arr = YiiComm::executeByMethod($modelClass,'get_user_by_phone',$_POST);

    $data = $arr->getModels();
    if(count($data) > 0){
        foreach($data as $val){
            $array = array('uid'=>$val['uid'],'groupid'=>$val['groupid'],'username'=>$val['username'],'password'=>$val['password'],'email'=>$val['email'],'phonenum'=>$val['phonenum'],'dateline'=>$val['dateline'],'updatetime'=>$val['updatetime'],'lastlogin'=>$val['lastlogin'],'ip'=>$val['ip'],'salt'=>$val['salt']);
        }
        $arr = $array;
    }else{
        $arr = 0;
    }
//	global $_SGLOBAL;
//	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('user')." WHERE phonenum='$phonenum'");
//	$arr = mysql_fetch_array($query, $result_type = MYSQL_ASSOC);
	return $arr;
}

function get_agent_by_username($username) {
	global $_SGLOBAL;
	$query = $_SGLOBAL['db']->query("SELECT *,t_user_stock.stock_id FROM t_sys_user 
                                    left JOIN t_user_stock ON t_sys_user.uuid = t_user_stock.user_id 
                                    WHERE phone_number='".$username."' AND user_status = 0 group by t_sys_user.uuid");
	$arr = mysql_fetch_array($query, $result_type = MYSQL_ASSOC);
	return $arr;
}

function check_usernameexists($username) {
	global $_SGLOBAL;
	$query = $_SGLOBAL['db']->query('SELECT phone_number FROM t_sys_user WHERE phone_number=\''.$username.'\'');
	$data = @mysql_result($query, 0);
	return $data;
}

function check_emailformat($email) {
	return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
}

function check_emailexists($email, $phonenum = '') {

    $_POST['phone'] = $phonenum;
    $_POST['email'] = $email;
    $modelClass = 'app\com\simtoon\models\User';
    $data = YiiComm::executeByMethod($modelClass,'check_email',$_POST);
    $arr = $data->getModels();

//	global $_SGLOBAL;
//	$sqladd = $phonenum !== '' ? "AND phonenum<>'$phonenum'" : '';
//	$query = $_SGLOBAL['db']->query("SELECT email FROM  ".tname('user')." WHERE email='$email' $sqladd");
//	$email = @mysql_result($query, 0);
	return $arr[0]['email'];
}

//用户修改密码
function edit_user($username, $oldpw, $newpw,$phonenum) {

    $_POST['username'] = $username;
    $modelClass = 'app\com\simtoon\models\User';
    $arr = YiiComm::executeByMethod($modelClass,'get_user_by_username',$_POST);

    $data = $arr->getModels();
    if(count($data) > 0){
        foreach($data as $val){
            $array = array('uid'=>$val['uid'],'username'=>$val['username'],'password'=>$val['password'],'salt'=>$val['salt']);
        }

        if(trim(strtoupper(md5(md5($oldpw).$array['salt']))) != trim($array['password'])){
            return -1;
        }else{

            if($newpw){
                $_POST['oldpw'] = $array['password'];
                $_POST['newpw'] = $newpw;
                $_POST['username'] = $username;
                $_POST['salt'] = $array['salt'];
                $_POST['phonenum'] = $phonenum;

                $modelClass = 'app\com\simtoon\models\User';
                $res = YiiComm::executeByMethod($modelClass, "updatePassword", $_POST);
                return $res;
            }else{
                return -7;
            }
        }
    }else{
        $arr = 0;
    }

//	global $_SGLOBAL;
//	$data = $_SGLOBAL['db']->query("SELECT username, uid, password, salt FROM ".tname(user)." WHERE username='$username'");
//	$data = mysql_fetch_array($data, $result_type = MYSQL_ASSOC);
//
//	if(trim(strtoupper(md5(md5($oldpw).$data['salt']))) != trim($data['password'])){
//		return -1;
//	}else{
//		$sqladd = $newpw ? "password='".strtoupper(md5(md5($newpw).$data['salt']))."'" : '';
//		if($sqladd || $emailadd) {
//			$_SGLOBAL['db']->query("UPDATE ".tname(user)." SET $sqladd WHERE username='$username'");
//			return $_SGLOBAL['db']->affected_rows();
//		} else {
//			return -7;
//		}
//	}
}











//其他经纪人公司修改密码
function edit_users($username, $oldpw, $newpw) {

    $_POST['username'] = $username;
    $modelClass = 'app\com\simtoon\models\User';
    $arr = YiiComm::executeByMethod($modelClass,'get_users_by_username',$_POST);

    $data = $arr->getModels();
    if(count($data) > 0){
        foreach($data as $val){
            $array = array('uid'=>$val['uid'],'username'=>$val['username'],'password'=>$val['password'],'salt'=>$val['salt']);
        }
      if(trim(strtoupper(sha1($oldpw))) != trim($array['password'])){
            return -1;
        }else{

            if($newpw){
                $_POST['newpw'] = $newpw;
                $_POST['username'] = $username;
                $_POST['salt'] = $array['salt'];


                $modelClass = 'app\com\simtoon\models\User';

                $res = YiiComm::executeByMethod($modelClass, "updatePasswords", $_POST);
                return $res;
            }else{
                return -7;
            }
        }
    }else{
        $arr = 0;
    }


}
//更改其他公司经纪人密码
function setOtherUser($phonenum,$password){

    $_POST['password'] = $password;
    $_POST['phonenum'] = $phonenum;
    $modelClass = 'app\com\simtoon\models\User';
    $res = YiiComm::executeByMethod($modelClass, "otherLostPassword", $_POST);

    return $res;


}


    //检验是否第三方员工工号
    function check_usernames($username){
        $_POST['username']=$username;
     $modelClass="app\com\simtoon\models\User";
     $data=YiiComm::executeByMethod($modelClass,'get_users_by_username',$_POST);
      $res=$data->getModels();
       if(count($res) > 0){
           return true;
       }
       else{
           return false;
       }

    }




/**
 * 查询注册者是否为经纪人客户，若为经纪人客户则更新 fang_customers 为已注册
 * @param $phonenum
 */
function setCustomer($phonenum){

    $_POST['phone'] = $phonenum;
    $modelClass = 'app\com\simtoon\models\User';
    $data = YiiComm::executeByMethod($modelClass,'queryCustomer',$_POST);
    $res = $data->getModels();
//    global $_SGLOBAL;
//    $data = $_SGLOBAL['db']->query("SELECT id,client_no,owner_no,client_mobile FROM fang_customers WHERE client_mobile='$phonenum'");
//    $data = mysql_fetch_array($data, $result_type = MYSQL_ASSOC);
    try{
        if(count($res) > 0){
            $_POST['id'] = $res[0]['client_id'];
            $modelClass = 'app\com\simtoon\models\User';
            $res = YiiComm::executeByMethod($modelClass, "setCustomer", $_POST);

        }

//        if($data){
//            $id = $data['id'];
//            $_SGLOBAL['db']->query("UPDATE fang_customers SET is_regist = 1 WHERE id= ".$id);
//           $res = $_SGLOBAL['db']->affected_rows();
//
//        }
    }catch (Exception $e){
        $e->getMessage();
        return false;
    }
    if($res > 0){
        return true;
    }else{
        return false;
    }

}

//更改用户密码
function setUser($phonenum,$password){

   // $arr = get_user_by_username($phonenum);
   // if(count($arr) > 0){
        $_POST['password'] = $password;
        $_POST['phonenum'] = $phonenum;
        $modelClass = 'app\com\simtoon\models\User';
        $res = YiiComm::executeByMethod($modelClass, "lostPassword", $_POST);

 //   }
    return $res;
//    if($res > 0){
//        return true;
//    }else{
//        return false;
//    }
//    global $_SGLOBAL;
//    $data = $_SGLOBAL['db']->query("SELECT username, uid, password, salt FROM supe_user WHERE phonenum='$phonenum'");
//    $data = mysql_fetch_array($data, $result_type = MYSQL_ASSOC);
//    try{
//        if(count($data) > 0){
//            $password=strtoupper(md5(md5($password).$data['salt']));
//            $id = $data['uid'];
//            $_SGLOBAL['db']->query("UPDATE supe_user SET password =' ".$password."'  WHERE uid= ".$id);
//            $res = $_SGLOBAL['db']->affected_rows();
//
//        }
//    }catch (Exception $e){
//        $e->getMessage();
//        return false;
//    }

}

//查询是否为注册用户
//function setUsers($phonenum){
//
//
//    global $_SGLOBAL;
//    $data = $_SGLOBAL['db']->query("SELECT username, uid, password, salt FROM supe_user WHERE phonenum='$phonenum'");
//    $data = mysql_fetch_array($data, $result_type = MYSQL_ASSOC);
//    if($data ){
//    return true;
//    }
//    else {return false;}
//
//}


?>
